{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [],
   "source": [
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "data": {
      "text/plain": "  货号  平台  销量\n0  A  淘宝   1\n1  B  淘宝   2\n2  B  京东   3\n3  A  京东   4\n4  B  淘宝   5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>货号</th>\n      <th>平台</th>\n      <th>销量</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>A</td>\n      <td>淘宝</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>B</td>\n      <td>淘宝</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>B</td>\n      <td>京东</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>A</td>\n      <td>京东</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>B</td>\n      <td>淘宝</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    '货号': ['A','B','B','A','B'],\n",
    "    '平台': ['淘宝','淘宝','京东',\n",
    "               '京东','淘宝'],\n",
    "    '销量': [1, 2, 3, 4, 5]\n",
    "})\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "data": {
      "text/plain": "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x12570a6d0>"
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gb = df.groupby('货号')\n",
    "gb"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "  货号  平台  销量\n",
      "0  A  淘宝   1\n",
      "3  A  京东   4\n",
      "B\n",
      "  货号  平台  销量\n",
      "1  B  淘宝   2\n",
      "2  B  京东   3\n",
      "4  B  淘宝   5\n"
     ]
    }
   ],
   "source": [
    "for name, group in gb:\n",
    "    print(name)\n",
    "    print(group)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "data": {
      "text/plain": "<pandas.core.groupby.generic.SeriesGroupBy object at 0x1259a98d0>"
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sgb = df.groupby('货号')['销量']\n",
    "sgb"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "0    1\n",
      "3    4\n",
      "Name: 销量, dtype: int64\n",
      "B\n",
      "1    2\n",
      "2    3\n",
      "4    5\n",
      "Name: 销量, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "for name, group in sgb:\n",
    "    print(name)\n",
    "    print(group)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "    销量\n货号    \nA    5\nB   10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n    </tr>\n    <tr>\n      <th>货号</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gb.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "          销量\n货号          \nA   2.500000\nB   3.333333",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n    </tr>\n    <tr>\n      <th>货号</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>2.500000</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>3.333333</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gb.mean()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [
    {
     "data": {
      "text/plain": "  货号  平台  销量  分组求和\n0  A  淘宝   1     5\n1  B  淘宝   2    10\n2  B  京东   3    10\n3  A  京东   4     5\n4  B  淘宝   5    10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>货号</th>\n      <th>平台</th>\n      <th>销量</th>\n      <th>分组求和</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>A</td>\n      <td>淘宝</td>\n      <td>1</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>B</td>\n      <td>淘宝</td>\n      <td>2</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>B</td>\n      <td>京东</td>\n      <td>3</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>A</td>\n      <td>京东</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>B</td>\n      <td>淘宝</td>\n      <td>5</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['分组求和'] = gb['销量'].transform('sum')\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "   平台  销量\n1  淘宝   2\n2  京东   3\n4  淘宝   5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>平台</th>\n      <th>销量</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>淘宝</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>京东</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>淘宝</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = gb.filter(lambda x: x['销量'].sum() > 6)\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "   平台  销量 货号\n1  淘宝   2  B\n2  京东   3  B\n4  淘宝   5  B",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>平台</th>\n      <th>销量</th>\n      <th>货号</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>淘宝</td>\n      <td>2</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>京东</td>\n      <td>3</td>\n      <td>B</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>淘宝</td>\n      <td>5</td>\n      <td>B</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.join(df['货号'])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}